﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraLayout.Utils;
using ProgramMain.Database;

namespace ProgramMain.Classifiers
{
    public partial class CurveElementTypeDetail : ProgramMain.Framework.BaseModalDetailCtl
    {
        public CurveElementTypeDetail()
        {
            InitializeComponent();
        }

        public Nullable<int> CurveElementKindID = new Nullable<int>();

        protected override void InitializeDataControls()
        {
            if (!CurveElementKindID.HasValue)
            {
                this.curveElementKindsTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
                this.curveElementKindsTableAdapter.Fill(this.simpleOpticNetDB.CurveElementKinds);
            }

            if (ShowMode == ShowModeTypes.Add)
            {
                SimpleOpticNetDB.CurveElementTypesRow row = (SimpleOpticNetDB.CurveElementTypesRow)CurrentRowView.Row;
                if (CurveElementKindID != null && CurveElementKindID.HasValue)
                {
                   if(row != null)  row.CurveElementKindID = CurveElementKindID.Value;
                }
                else
                {
                    if (this.curveElementKindsBindingSource.Count > 0)
                    {
                        this.curveElementKindsBindingSource.MoveFirst();
                        SimpleOpticNetDB.CurveElementKindsRow curveElementKindsRow = (SimpleOpticNetDB.CurveElementKindsRow)((DataRowView)this.curveElementKindsBindingSource.Current).Row;
                        if (row != null && curveElementKindsRow != null)
                        row.CurveElementKindID = curveElementKindsRow.ID;
                    }
                }
            }

            string Caption;
            if (CurveElementKindID.HasValue && CurveElementKindID.Value == 1)
            {
                Caption = "Тип кабеля";
            }
            else if (CurveElementKindID.HasValue && CurveElementKindID.Value == 2)
            {
                Caption = "Тип муфты";
            }
            else if (CurveElementKindID.HasValue && CurveElementKindID.Value == 2)
            {
                Caption = "Тип оптического кросса";
            }
            else
            {
                Caption = "Класс оборудования";
            }
            
            labelCaption.Text = Caption;
        }

        protected override void BindDataControls()
        {
            if (!CurveElementKindID.HasValue)
            {
                this.lookupCurveElementKindID.DataBindings.Add(new Binding("EditValue", this.dataSource, "CurveElementKindID"));
            }
            else
            {
                dxValidationProvider.SetValidationRule(this.lookupCurveElementKindID, null);
                this.layoutControlCurveElementKindID.Visibility = LayoutVisibility.Never;
            }

            this.txtEditCaption.DataBindings.Add(new Binding("Text", this.dataSource, "Caption"));
            this.txtModuleCount.DataBindings.Add(new Binding("Text", this.dataSource, "ModuleCount"));
            this.txtFibreCount.DataBindings.Add(new Binding("Text", this.dataSource, "FibreCount"));
        }
    }
}
